<template>
  <div class='user-profile'>
     <van-nav-bar
      title="个人信息"
      left-arrow
      class="app-nav-bar"
      @click-left='$router.back()'
    >
    </van-nav-bar>

    <van-cell title="头像" is-link center>
      <van-image
        width="30"
        height="30"
        round
        fit="cover"
        :src="user.photo"
        @click="$refs.file.click()"
      />
    </van-cell>
    <!-- 点击触发这个input (隐藏)-->
    <input type="file" hidden ref="file" accept="image/*" @change="onFileCahnge"/>
    <van-cell
      title="昵称"
      is-link
      :value="user.name"
      @click='isEditNameShow = true'
    ></van-cell>
    <van-cell
      title="性别"
      is-link
      @click="isEditGenderShow = true"
      :value="user.gender === 0 ? '男' : '女'"
    ></van-cell>
    <van-cell
      title="生日"
      is-link
      @click="isEditBirthdayShow = true"
      :value="user.birthday"
    >
    </van-cell>

    <!-- 注意: 弹出层关闭, 里面的数据并不销毁 可以使用v-if -->
    <van-popup
      v-model="isEditNameShow"
      position="bottom"
      :style="{ height: '100%' }"
    >
      <update-name
        v-if='isEditNameShow'
        @close='isEditNameShow = false'
        :name='user.name'
        @update-name='user.name = $event'
      />
    </van-popup>

    <!-- 性别弹框 -->
    <van-popup
      v-model="isEditGenderShow"
      position="bottom"
    >
      <update-gender
       v-model="user.gender"
       v-if="isEditGenderShow"
       @close='isEditGenderShow = false'
      >
      </update-gender>
    </van-popup>

    <!-- 生日弹框 -->
    <van-popup
      v-model="isEditBirthdayShow"
      position="bottom"
    >
      <update-birthday
        v-model="user.birthday"
        v-if="isEditBirthdayShow"
        @close='isEditBirthdayShow = false'
      >
      </update-birthday>
    </van-popup>

    <!-- 修改用户头像 -->
    <van-popup
      class="update-photo-popup"
      v-model="isEditPhotoShow"
      position="bottom"
      :style="{height: '100%'}"
    >
      <update-photo
        :file='file'
        v-if="isEditPhotoShow"
        @close='isEditPhotoShow = false'
        @update-photo='user.photo = $event'
      >
      </update-photo>
    </van-popup>

  </div>
</template>

<script>
import { getUserProfile } from '@/api/user'
import UpdateName from './components/update-name'
import UpdateGender from './components/update-gender'
import UpdateBirthday from './components/update-birthday'
import UpdatePhoto from './components/update-photo'
export default {
  name: 'UserProfile',
  components: {
    UpdateName,
    UpdateGender,
    UpdateBirthday,
    UpdatePhoto
  },
  props: {},
  data () {
    return {
      user: {}, // 用户信息
      isEditNameShow: false,
      isEditGenderShow: false,
      isEditBirthdayShow: false,
      isEditPhotoShow: false,
      file: null
    }
  },
  computed: {},
  watch: {},
  mounted () {
    this.loadUserProfile()
  },
  methods: {
    async loadUserProfile () {
      const { data } = await getUserProfile()
      this.user = data.data
    },
    // 监听选择图片; 注意: 选择相同图片不会触发( 要将表单的value )清空后才会再次触发
    onFileCahnge () {
      this.file = this.$refs.file.files[0] // 获取file对象
      this.isEditPhotoShow = true
      this.$refs.file.value = ''
    }
  }
}

</script>
<style lang='less' scoped>
.van-popup {
  background-color: #f5f7f9;
}
.update-photo-popup {
  background-color: #000;
}
</style>
